package cn.wgx.safetyhelmet.web;


import cn.wgx.safetyhelmet.entity.User;
import cn.wgx.safetyhelmet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    @Qualifier("userServiceJpaImp")
    private UserService userService;

    @GetMapping
    public List<User> getUserList(@ModelAttribute User user){
        return userService.getUsers(user);
    }

    @GetMapping("/count")
    public Integer getUserCount(){
        return userService.getAllUsers();
    }

    @PostMapping
    public User insertUser(@RequestBody User user){
        if(userService.add(user) != null ){
            return user;
        }else{
            return null;
        }
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id){

        return userService.getUserById(id);
    }

    @PutMapping("/{id}")
    public String putUserByUd(@PathVariable Long id, @ModelAttribute User user){
        user.setId(id);
        if(userService.update(user) > 0){
            return "success";
        }
        return "fail";
    }

    @DeleteMapping("/{id}")
    public String delUserById(@PathVariable Long id){
        if(userService.del(id) > 0){
            return "success";
        }
        return "fail";
    }

}
